package de.jmda.mview.fx.node.typeshape.relation;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.geometry.Point2D;
import javafx.scene.input.MouseEvent;
import de.jmda.fx.node.Line;
import de.jmda.fx.node.Line.BehaviourMouseDraggingData;
import de.jmda.fx.node.behaviour.BehaviourMouseDraggingConstraint;
import de.jmda.fx.node.behaviour.DraggingCalculatorDefault;
import de.jmda.fx.node.behaviour.DraggingData;
import de.jmda.fx.node.behaviour.DraggingCalculator;

public abstract class RelationSegmentOrthogonal extends RelationSegment
{
	public RelationSegmentOrthogonal(
			ObjectProperty<Point2D> start,
      ObjectProperty<Point2D> xyEndProvider)
  {
		super(start, xyEndProvider);
  }

	protected DraggingData getLineDraggingData(
			MouseEvent mouseEventCurrent,
			Line line,
			BehaviourMouseDraggingData behaviourMouseDraggingData)
	{
		// store current mouse event coordinates
		double mouseEventCurrentX = mouseEventCurrent.getSceneX();
		double mouseEventCurrentY = mouseEventCurrent.getSceneY();
		
		// shortcut to most recent mouse coordinates
		double mouseEventMostRecentX = behaviourMouseDraggingData.getMouseDraggingMostRecent().getSceneX();
		double mouseEventMostRecentY = behaviourMouseDraggingData.getMouseDraggingMostRecent().getSceneY();
		
		// create line dragging data without taking constraint into account
		DraggingData lineDraggingDataRequest =
				new DraggingData(
						mouseEventCurrentX - mouseEventMostRecentX,
						mouseEventCurrentY - mouseEventMostRecentY);

		BehaviourMouseDraggingConstraint lineDraggingConstraint =
				new BehaviourMouseDraggingConstraint(line);
		
		DraggingCalculator lineDraggingCalculator =
				new DraggingCalculatorDefault(
						line, lineDraggingDataRequest, lineDraggingConstraint);

		// let the calculator create line dragging data with regard to constraint
		return lineDraggingCalculator.calculate();
	}
}